package com.example.spotitube.View;

import java.util.ArrayList;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.spotitube.R;
import com.example.spotitube.Connection.Connection;
import com.example.spotitube.Controller.ListAdapter;
import com.example.spotitube.Model.Track;


/**
 * @author Rick
 * 
 * shows a list of tracks of a specific album that was passed in the Intent
 *
 */
public class AlbumDetailActivity extends ListActivity{
	
	private ProgressDialog	progressDialog;
	private Handler			handler;
	
	
	public void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		
		setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_PORTRAIT );
		requestWindowFeature( Window.FEATURE_NO_TITLE );
		getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		
		setContentView( R.layout.album_detail );
		
		String albumName = ( String ) getIntent().getSerializableExtra( "albumName" );
		TextView albumNameField = ( TextView ) findViewById( R.id.albumName );
		albumNameField.setText( albumName );
		
		setListAdapter( new ListAdapter( this, R.layout.list_item, new ArrayList<Track>() ) );
		
		handler = new Handler( new Handler.Callback(){
			
			@Override
			public boolean handleMessage( Message msg ) {
				switch( msg.what ){
					case -1:
						
						break;
					case 1:
						setListAdapter( new ListAdapter( getApplicationContext(), R.layout.list_item, ( ArrayList<Track> ) msg.obj ) );
						( ( ArrayAdapter<Track> ) getListAdapter() ).notifyDataSetChanged();
						handler.removeMessages( 1 );
						break;
					default:
						break;
				}
				progressDialog.dismiss();
				return false;
			}
		} );
		new Connection( handler ).execute( albumName , "album" );
		
		progressDialog = ProgressDialog.show( this, "", "Retrieving data..." );
		progressDialog.setCancelable( true );
		
	}
	
	
	@Override
	public void onListItemClick( ListView l, View v, int position, long id ) {
		Intent intent = new Intent( this, TrackDetailActivity.class );
		Track track = ( Track ) l.getItemAtPosition( position );
		intent.putExtra( "track", track );
		startActivity( intent );
	}
	
}
